using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_PDH
    {
        public clsCHI_TIET_PDH(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }
        #region Private methods
        private CHI_TIET_PDH GetDataRow(DataRow row)
        {
            CHI_TIET_PDH objCHI_TIET_PDH = new CHI_TIET_PDH();
            try
            {
                if (row["MaChiTiet"] != DBNull.Value)
                    objCHI_TIET_PDH.MaChiTiet = int.Parse(row["MaChiTiet"].ToString());
            }
            catch { }
            try
            {
                if (row["MaThietBi"] != DBNull.Value)
                    objCHI_TIET_PDH.MaThietBi = ((row["MaThietBi"]).ToString());
            }
            catch { }
            try
            {
                if (row["POThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PDH.POThamChieuSAP = (row["POThamChieuSAP"].ToString());
            }
            catch { }
            try
            {
                if (row["TinhTrang"] != DBNull.Value)
                    objCHI_TIET_PDH.TinhTrang = int.Parse(row["TinhTrang"].ToString());
            }
            catch { }
            try
            {
                if (row["SoLuong"] != DBNull.Value)
                    objCHI_TIET_PDH.SoLuong = float.Parse(row["SoLuong"].ToString());
            }
            catch { }
            try
            {
                if (row["NgayGiaoHang"] != DBNull.Value)
                    objCHI_TIET_PDH.NgayGiaoHang = Convert.ToDateTime(row["NgayGiaoHang"]);
            }
            catch { }
            try
            {
                if (row["TenVTPT"] != DBNull.Value)
                    objCHI_TIET_PDH.TenVTPT = (row["TenVTPT"].ToString());
            }
            catch { }
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objCHI_TIET_PDH.MaTrangThai = int.Parse(row["MaTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["POItem_No"] != DBNull.Value)
                    objCHI_TIET_PDH.POItem_No = int.Parse(row["POItem_No"].ToString());
            }
            catch { }
            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objCHI_TIET_PDH.TenTrangThai = (row["TenTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["AccAssignment"] != DBNull.Value)
                    objCHI_TIET_PDH.AccAssignment = (row["AccAssignment"].ToString());
            }
            catch { }

            try
            {
                if (row["MaTaiSan"] != DBNull.Value)
                    objCHI_TIET_PDH.MaTaiSan = (row["MaTaiSan"].ToString());
            }
            catch { }

            try
            {
                if (row["MaPhieuIn"] != DBNull.Value)
                    objCHI_TIET_PDH.oThongTinNhan.MaPhieuIn = int.Parse(row["MaPhieuIn"].ToString());
            }
            catch { }

            try
            {
                if (row["MaKho"] != DBNull.Value)
                    objCHI_TIET_PDH.oThongTinNhan.MaKho = (row["MaKho"].ToString());
            }
            catch { }
            try
            {
                if (row["MaKho"] != DBNull.Value)
                    objCHI_TIET_PDH.MaKho = (row["MaKho"].ToString());
            }
            catch { }
            try
            {
                if (row["SoLuongIn"] != DBNull.Value)
                    objCHI_TIET_PDH.SoLuongIn = int.Parse(row["SoLuongIn"].ToString());
            }
            catch { }
            try
            {
                if (row["MaNhaSanXuat"] != DBNull.Value)
                    objCHI_TIET_PDH.MaNhaSanXuat = (row["MaNhaSanXuat"].ToString());
            }
            catch { }
            try
            {
                if (row["TenNhaSanXuat"] != DBNull.Value)
                    objCHI_TIET_PDH.TenNhaSanXuat = (row["TenNhaSanXuat"].ToString());
            }
            catch { }
            try
            {
                if (row["TenNhomVTPT"] != DBNull.Value)
                    objCHI_TIET_PDH.TenNhomVTPT = (row["TenNhomVTPT"].ToString());
            }
            catch { }
            return objCHI_TIET_PDH;
        }
        private List<CHI_TIET_PDH> GetCHI_TIET_PDH(DataTable dt)
        {
            List<CHI_TIET_PDH> lst = new List<CHI_TIET_PDH>();
            CHI_TIET_PDH obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);               
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        #region Public methods
        public List<CHI_TIET_PDH> usp_SelectCHI_TIET_PDHByPOThamChieuSAP(string POThamChieuSAP)
        {
            string spName = "usp_SelectCHI_TIET_PDHByPOThamChieuSAP";
            DataTable dt = new DataTable();
            List<CHI_TIET_PDH> lst = new List<CHI_TIET_PDH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@POThamChieuSAP", POThamChieuSAP);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PDH(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public CHI_TIET_PDH usp_SelectCHI_TIET_PDH(int MaChiTiet)
        {
            string spName = "usp_SelectCHI_TIET_PDH";
            DataTable dt = new DataTable();
            List<CHI_TIET_PDH> lst = new List<CHI_TIET_PDH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChiTiet", MaChiTiet);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PDH(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            if (lst.Count > 0)
                return lst[0];
            return null;
        }

        //public int usp_InsertCHI_TIET_PYC(CHI_TIET_PDH obj)
        //{
        //    int iResult = 0;
        //    string spName = "usp_InsertCHI_TIET_PDH";
        //    SqlParameter[] par;
        //    try
        //    {
        //        par = new SqlParameter[12];
        //        par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
        //        par[0].Direction = ParameterDirection.ReturnValue;
        //        par[1] = new SqlParameter("@MaChiTiet", lstCT[i].MaChiTiet);
        //        par[2] = new SqlParameter("@POThamChieuSAP", lstCT[i].POThamChieuSAP);
        //        par[3] = new SqlParameter("@MaThietBi", lstCT[i].MaThietBi);
        //        par[4] = new SqlParameter("@SoLuong", lstCT[i].SoLuong);
        //        par[5] = new SqlParameter("@TinhTrang", lstCT[i].TinhTrang);
        //        par[6] = new SqlParameter("@NgayGiaoHang", lstCT[i].NgayGiaoHang);
        //        par[7] = new SqlParameter("@NguoiTao", lstCT[i].NguoiTao);
        //        par[8] = new SqlParameter("@NgayTao", lstCT[i].NgayTao);
        //        par[9] = new SqlParameter("@MaTrangThai", lstCT[i].MaTrangThai);
        //        par[10] = new SqlParameter("@POItemNo", lstCT[i].POItem_No);
        //        par[11] = new SqlParameter("@AccAssignment", lstCT[i].AccAssignment);


        //        if (SqlHelper.IsHaveTran())
        //            iResult = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
        //        else
        //            iResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, spName, par);

        //    }
        //    catch (Exception ex)
        //    {
        //    }
        //    return iResult;
        //}


        #endregion Public methods
    }
}
